﻿using HML.Design;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;

namespace HML
{

    /// <summary>
    /// 滑块集合
    /// </summary>
    [Editor(typeof(MultidropSlideThumbCollectionEditor), typeof(UITypeEditor))]
    public class MultidropSlideThumbCollection : IList, ICollection, IEnumerable
    {
        #region

        private ArrayList multidropSlideLumpList = new ArrayList();

        private MultidropSlide owner;

        #endregion

        internal MultidropSlideThumbCollection(MultidropSlide owner)
        {
            this.owner = owner;
        }

        #region IEnumerable

        public IEnumerator GetEnumerator()
        {
            MultidropSlideThumb[] listArray = new MultidropSlideThumb[this.Count];
            for (int index = 0; index < listArray.Length; ++index)
            {
                listArray[index] = (MultidropSlideThumb)this.multidropSlideLumpList[index];
            }
            return listArray.GetEnumerator();
        }

        #endregion

        #region ICollection

        public void CopyTo(Array array, int index)
        {
            for (int i = 0; i < this.Count; i++)
            {
                array.SetValue(this.multidropSlideLumpList[i], i + index);
            }
        }

        public int Count
        {
            get { return this.multidropSlideLumpList.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return (object)this; }
        }

        #endregion

        #region IList

        public int Add(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is MultidropSlideThumb))
            {
                throw new ArgumentException("MultidropSlideThumb");
            }
            return this.Add((MultidropSlideThumb)value);
        }

        public int Add(MultidropSlideThumb lump)
        {
            if (lump == null)
            {
                throw new ArgumentNullException("lump");
            }
            if (lump.Owner != null)
            {
                throw new ArgumentException("MultidropSlideThumb滑块不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            lump.Owner = this.owner;
            decimal current_lump_value_tmp = (decimal)lump.SelfValue;
            current_lump_value_tmp = Math.Max(current_lump_value_tmp, (decimal)this.owner.MinValue);
            current_lump_value_tmp = Math.Min(current_lump_value_tmp, (decimal)this.owner.MaxValue);
            lump.OnlySetValue((double)current_lump_value_tmp);

            int insert_index = 0;
            ArrayList multidropSlideLumpList_tmp = new ArrayList();
            if (this.multidropSlideLumpList.Count == 0)
            {
                multidropSlideLumpList_tmp.Add(lump);
            }
            else
            {
                //获取插入的索引
                for (int i = 0; i < multidropSlideLumpList.Count; i++)
                {
                    if (current_lump_value_tmp >= (decimal)((MultidropSlideThumb)this.multidropSlideLumpList[i]).SelfValue)
                    {
                        insert_index += 1;
                    }
                    else
                    {
                        break;
                    }
                }
                //创建新集合
                if (insert_index == 0)
                {
                    multidropSlideLumpList_tmp.Add(lump);
                }
                for (int i = 0; i < this.multidropSlideLumpList.Count; i++)
                {
                    multidropSlideLumpList_tmp.Add(this.multidropSlideLumpList[i]);
                    if (i + 1 == insert_index)
                    {
                        multidropSlideLumpList_tmp.Add(lump);
                    }
                }
            }

            this.multidropSlideLumpList = multidropSlideLumpList_tmp;
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
            return insert_index;
        }

        public void Clear()
        {
            for (int i = 0; i < this.multidropSlideLumpList.Count; i++)
            {
                ((MultidropSlideThumb)this.multidropSlideLumpList[i]).Owner = null;
            }
            this.multidropSlideLumpList.Clear();
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public bool Contains(object value)
        {
            if (value == null)
                throw new ArgumentNullException("value");
            return this.IndexOf(value) != -1;
        }

        bool IList.Contains(object value)
        {
            if (value is MultidropSlideThumb)
            {
                return this.Contains((MultidropSlideThumb)value);
            }
            return false;
        }

        public int IndexOf(object value)
        {
            if (value is MultidropSlideThumb)
            {
                return this.multidropSlideLumpList.IndexOf(value);
            }
            return -1;
        }

        public void Insert(int index, object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is MultidropSlideThumb))
            {
                throw new ArgumentException("类型应该为 MultidropSlideThumb");
            }
            MultidropSlideThumb lump = (MultidropSlideThumb)value;
            if (lump.Owner != null)
            {
                throw new ArgumentException("MultidropSlideThumb滑块不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            this.Insert(index, (MultidropSlideThumb)value);
        }

        public void Insert(int index, MultidropSlideThumb lump)
        {
            if (lump == null)
            {
                throw new ArgumentNullException("lump");
            }
            if (lump.Owner != null)
            {
                throw new ArgumentException("MultidropSlideThumb滑块不应该同时被添加到多个地方，请先从原有地方移除再添加");
            }

            decimal current_lump_value_tmp = (decimal)lump.SelfValue;
            current_lump_value_tmp = Math.Max(current_lump_value_tmp, (decimal)this.owner.MinValue);
            current_lump_value_tmp = Math.Min(current_lump_value_tmp, (decimal)this.owner.MaxValue);
            lump.OnlySetValue((double)current_lump_value_tmp);

            if (index > this.Count)
            {
                index = this.Count;
            }
            if (
                (index > 0 && lump.SelfValue < ((MultidropSlideThumb)this.multidropSlideLumpList[index - 1]).SelfValue) ||
                (index < this.multidropSlideLumpList.Count - 1 && lump.SelfValue > ((MultidropSlideThumb)this.multidropSlideLumpList[index + 1]).SelfValue)
                )
            {
                throw new ArgumentException("要插入的滑块的值必须在前滑块与后滑块范围内");
            }

            ArrayList multidropSlideLumpList_tmp = new ArrayList();
            for (int i = 0; i < index; i++)
            {
                multidropSlideLumpList_tmp.Add(this.multidropSlideLumpList[i]);
            }
            lump.Owner = this.owner;
            multidropSlideLumpList_tmp.Add(lump);
            for (int i = index; i < this.Count; i++)
            {
                multidropSlideLumpList_tmp.Add(this.multidropSlideLumpList[i]);
            }
            this.multidropSlideLumpList = multidropSlideLumpList_tmp;
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public bool IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Remove(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            if (!(value is MultidropSlideThumb))
            {
                throw new ArgumentException("MultidropSlideThumb");
            }
            this.Remove((MultidropSlideThumb)value);
        }

        public void Remove(MultidropSlideThumb lump)
        {
            if (lump == null)
            {
                throw new ArgumentNullException("lump");
            }

            lump.Owner = null;
            this.multidropSlideLumpList.Remove(lump);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public void RemoveAt(int index)
        {
            ((MultidropSlideThumb)this.multidropSlideLumpList[index]).Owner = null;
            this.multidropSlideLumpList.RemoveAt(index);
            if (this.owner != null)
            {
                this.owner.InitializeElement();
                this.owner.Invalidate();
            }
        }

        public MultidropSlideThumb this[int index]
        {
            get { return (MultidropSlideThumb)this.multidropSlideLumpList[index]; }
            private set { throw new NotImplementedException("不允许直接修改滑块引用，请使用 Remove、Add 方式添加"); }
        }

        object IList.this[int index]
        {
            get { return (object)this.multidropSlideLumpList[index]; }
            set { throw new NotImplementedException("不允许直接修改滑块引用，请使用 Remove、Add 方式添加"); }
        }

        #endregion

    }

}
